package com.jiankang.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import net.sf.json.JSONArray;


public class Data {
	
	public static String cid;
	
	static {
		//读取类路径下的配置文件
		try {
			Properties p = getResourceAsProperties("data.properties");
			cid = p.getProperty("cid");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static final class URL {
		public static String UploadURL;
		public static String base;
		public static String fileUpload;
		static {
			try {
				Properties p = getResourceAsProperties("data.properties");
				UploadURL = p.getProperty("url.UploadURL");
				base = p.getProperty("url.base");
				fileUpload = p.getProperty("url.fileUpload");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static final class JSON {
		public static JSONArray qqq;
		
		static {
			try {
				Properties p = getResourceAsProperties("data.properties");
				qqq = JSONArray.fromObject(p.getProperty("json.qqq"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	
	

	/**
	 * 从file:/ 格式的文件路径下读取配置文件
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Properties getUrlAsProperties(String url) throws Exception {
		InputStream is = new FileInputStream(url);
		Properties p = new Properties();
		p.load(is);
		is.close();
		return p;
	}
	
	/**
	 * 读取类路径下的配置文件
	 * @param resource
	 * @return
	 * @throws Exception
	 */
	public static Properties getResourceAsProperties(String resource) throws Exception {
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		Properties p = new Properties();
		p.load(is);
		is.close();
		return p;
	}
}
